//
//  XHHelper.m
//  微博表情键盘
//
//  Created by zxy on 2019/8/9.
//  Copyright © 2019 zxy. All rights reserved.
//

#import "XHHelper.h"

@implementation XHHelper

+(NSBundle *)bundle{
    static NSBundle *bundle;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        bundle = [NSBundle mainBundle];
    });
    return bundle;
}

+(NSString *)bundlePathFromBundleName:(NSString *)bundleName{
    return  [[self bundle] pathForResource:bundleName ofType:@"bundle"];
}

+(UIImage *)imageFromBundleName:(NSString *)bundleName
                      imageName:(NSString *)imgName{
    if(!bundleName || !imgName) return nil;
    NSString *bundlePath = [self bundlePathFromBundleName:bundleName];
    NSString *imgPath = [bundlePath stringByAppendingPathComponent:imgName];
    UIImage *image = [UIImage imageWithContentsOfFile:imgPath];
    return image;
}

+(UIImage *)imageFromBundleName:(NSString *)bundleName
                      imageName:(NSString *)imgName
                      inDirectory:(NSString *)directoryName{
    NSString *bundlePath = [self bundlePathFromBundleName:bundleName];
    /**此方法不知道为什么获取不到图片*/
//    NSString *strC = [[NSBundle bundleWithPath:bundlePath] pathForResource:imgName ofType:nil inDirectory:directoryName];
    NSString *strC = [[bundlePath stringByAppendingPathComponent:directoryName] stringByAppendingPathComponent:imgName];
    UIImage *image = [UIImage imageWithContentsOfFile:strC];
    return image;
}

#pragma mark -获取分组表情
+(NSArray<EmoticonModel *> *)groupEmoticons{
    static NSMutableArray *groups;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        groups = [NSMutableArray array];
        NSString *bundlePath = [XHHelper bundlePathFromBundleName:@"EmoticonWeibo"];
        NSString *groupEmoticonPath = [[NSBundle bundleWithPath:bundlePath] pathForResource:@"emoticons.plist" ofType:nil];
        NSDictionary *dic = [[NSDictionary alloc]initWithContentsOfFile:groupEmoticonPath];
        NSArray *groupEmoticons = dic[@"packages"];
        for (NSDictionary *emoticonDic in groupEmoticons) {
            EmoticonModel *groupEmModel = [EmoticonModel mj_objectWithKeyValues:emoticonDic];
            //加载分组表情info.plist文件通过组ID
            NSString *emoticonDirectoryPath = [bundlePath stringByAppendingPathComponent:groupEmModel.groupID];
            NSString *emoticonPlistPath = [emoticonDirectoryPath stringByAppendingPathComponent:@"info.plist"];
            NSDictionary *emDic = [NSDictionary dictionaryWithContentsOfFile:emoticonPlistPath];
            EmoticonModel *emModel = [EmoticonModel mj_objectWithKeyValues:emDic];
            [groups addObject:emModel];
        }
    });
    return groups;
}

+ (NSRegularExpression *)regexEmoticon {
    static NSRegularExpression *regex;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        regex = [NSRegularExpression regularExpressionWithPattern:@"\\[[^ \\[\\]]+?\\]" options:kNilOptions error:NULL];
    });
    return regex;
}

+ (NSDictionary *)emoticonDic {
    static NSMutableDictionary *dic;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        NSString *emoticonBundlePath = [[NSBundle mainBundle] pathForResource:@"EmoticonWeibo" ofType:@"bundle"];
        dic = [self _emoticonDicFromPath:emoticonBundlePath];
    });
    return dic;
}

+ (NSMutableDictionary *)_emoticonDicFromPath:(NSString *)path {
    NSMutableDictionary *dic = [NSMutableDictionary new];
    EmoticonModel *group = nil;
        NSString *plistPath = [path stringByAppendingPathComponent:@"info.plist"];
        NSDictionary *plist = [NSDictionary dictionaryWithContentsOfFile:plistPath];
        if (plist.count) {
            group = [EmoticonModel mj_objectWithKeyValues:plist];
        }
    for (XHEmoticon *emoticon in group.emoticons) {
        if (emoticon.png.length == 0) continue;
        NSString *pngPath = [path stringByAppendingPathComponent:emoticon.png];
        if (emoticon.chs) dic[emoticon.chs] = pngPath;
        if (emoticon.cht) dic[emoticon.cht] = pngPath;
    }
    
    NSArray *folders = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:path error:nil];
    for (NSString *folder in folders) {
        if (folder.length == 0) continue;
        NSDictionary *subDic = [self _emoticonDicFromPath:[path stringByAppendingPathComponent:folder]];
        if (subDic) {
            [dic addEntriesFromDictionary:subDic];
        }
    }
    return dic;
}


@end
